package com.macro.mall.portal.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.domain.HomeContentResult;
import com.macro.mall.domain.PmsProductDetailedResult;
import com.macro.mall.domain.PmsProductListResult;
import com.macro.mall.domain.ProductCategoryResult;
import com.macro.mall.model.PmsAlbumPic;
import com.macro.mall.model.PmsProduct;
import com.macro.mall.portal.service.HomeService;
import com.macro.mall.portal.service.PmsAlbumPicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 首页内容管理Controller
 * Created by macro on 2019/1/28.
 */
@RestController
@Api(tags = "HomeController", description = "首页内容管理")
@RequestMapping("/home")
public class HomeController {
    @Autowired
    private HomeService homeService;
    @Autowired
    private PmsAlbumPicService pmsAlbumPicServiceImpl;

    @ApiOperation("首页内容页信息展示（互换、好物、轮播广告、分类）")
    @RequestMapping(value = "/content", method = RequestMethod.GET)
    public CommonResult<HomeContentResult> content() {
        HomeContentResult contentResult = homeService.content();
        return CommonResult.success(contentResult);
    }

    @ApiOperation("分页获取好物商品")
    @RequestMapping(value = "/newProductList", method = RequestMethod.GET)
    public CommonResult<PmsProductListResult> newProductList(
            @RequestParam(value = "maxPrice", required = false) String maxPrice,
            @RequestParam(value = "minPrice", required = false) String minPrice,
            @RequestParam(value = "memberId", required = false) String memberId,
            @RequestParam(value = "productCategoryName", required = false) String productCategoryName,
            @RequestParam(value = "pageSize", defaultValue = "4") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PmsProductListResult pmsProductResult = new PmsProductListResult();
        Integer integer = homeService.newProductListCount(maxPrice, minPrice, productCategoryName);
        List<PmsProduct> productList = homeService.newProductList(maxPrice, minPrice, productCategoryName, pageSize, pageNum);
        pmsProductResult.setTotal(integer);
        pmsProductResult.setList(productList);
        return CommonResult.success(pmsProductResult);
    }

    @ApiOperation("分页获取互换商品")
    @RequestMapping(value = "/recommendProductList", method = RequestMethod.GET)
    public CommonResult<PmsProductListResult> recommendProductList(@RequestParam(value = "pageSize", defaultValue = "4") Integer pageSize,
                                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PmsProductListResult pmsProductResult = new PmsProductListResult();
        pmsProductResult.setTotal(homeService.recommendProductListCount());
        pmsProductResult.setList(homeService.recommendProductList(pageSize, pageNum));
        return CommonResult.success(pmsProductResult);
    }

    @ApiOperation("获取首页商品分类")
    @RequestMapping(value = "/productCateList", method = RequestMethod.GET)
    public CommonResult<List<ProductCategoryResult>> getProductCateList() {
        List<ProductCategoryResult> productCateList = homeService.getProductCateList();
        return CommonResult.success(productCateList);
    }

    @ApiOperation("根据id获取商品详情")
    @RequestMapping(value = "/getProductListById", method = RequestMethod.GET)
    public CommonResult<PmsProductDetailedResult> getProductListById(@RequestParam Long productId) {
        PmsProductDetailedResult pmsProductDetailedResult = new PmsProductDetailedResult();
        List<PmsAlbumPic> pmsAlbumPic = pmsAlbumPicServiceImpl.getPmsAlbumPic(productId);
        PmsProduct pmsProduct = homeService.productList(productId);
        if (pmsProduct == null) {
            CommonResult.failed("商品不存在或已下架！！！");
        }
        BeanUtils.copyProperties(pmsProduct, pmsProductDetailedResult);
        pmsProductDetailedResult.setPics(pmsAlbumPic);
        return CommonResult.success(pmsProductDetailedResult);
    }

//    @ApiOperation("根据分类获取专题")
//    @RequestMapping(value = "/subjectList", method = RequestMethod.GET)
//    @ResponseBody
//    public CommonResult<List<CmsSubject>> getSubjectList(@RequestParam(required = false) Long cateId,
//                                                         @RequestParam(value = "pageSize", defaultValue = "4") Integer pageSize,
//                                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
//        List<CmsSubject> subjectList = homeService.getSubjectList(cateId, pageSize, pageNum);
//        return CommonResult.success(subjectList);
//    }
}
